package io.jsen.meta.diff.support;

/**
 * @author jsen
 * @date 2021/4/28
 */
public class MetaModifier {
    public static final int COLLECTION = 0x00000001;
    public static final int MAP = 0x00000002;
    public static final int COMPLEX = 0x00000004;
    /**
     * 代表是否已经完成diff
     */
    public static final int COMPARED = 0x00000008;
    /**
     * 代表状态
     * 0 no change
     * 1 change
     * 2 insert
     * 3 delete
     * 0x00000010 + 0x00000020
     */
    public static final int STATE_0 = 0x00000010;
    public static final int STATE_1 = 0x00000020;

    public static boolean isCollection(int mod) {
        return (COLLECTION & mod) != 0;
    }

    public static boolean isMap(int mod) {
        return (MAP & mod) != 0;
    }

    public static boolean isComplex(int mod) {
        return (COMPLEX & mod) != 0;
    }

    public static boolean isContainer(int mod) {
        return isCollection(mod) || isMap(mod);
    }

    public static boolean anyComplex(int mod) {
        return isCollection(mod) || isMap(mod) || isComplex(mod);
    }

    public static boolean isCompared(int mod) {
        return (COMPARED & mod) != 0;
    }

    public static int setMod(int mod, int field, boolean on) {
        int current = mod & field;
        int target = on ? 1 : 0;
        if ((current ^ target) == 0) {
            // same
            return mod;
        }
        return mod ^ field;
    }

    private MetaModifier() {
    }
}
